﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hired
{
    public partial class viewjob : System.Web.UI.Page
    {
        JobseekerAccount ja;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)
                {
                    ViewState["RefUrl"] = Request.UrlReferrer.ToString();
                }
            }

            if (Request.QueryString["id"] != null)
            {
                string jobid = Request.QueryString["id"];
                if (User.Identity.IsAuthenticated)
                {
                    if (User.IsInRole("jobseeker"))
                    {
                        ja = DBJobseekerAccount.retrieveJobseekerAccount(User.Identity.Name, false);
                        if (DBJobseekerAccount.checkJobsWatchList(ja.JAccountID, jobid))
                        {
                            linkBtnAddToWatchList.Text = "Add to Watching List";
                        }
                        else
                        {
                            linkBtnAddToWatchList.Text = "Already exists in your Watching List";
                            linkBtnAddToWatchList.Enabled = false;
                        }
                    }
                    else if (User.IsInRole("company") || User.IsInRole("employer"))
                    {
                        btnApply.Visible = false;
                    }
                }
                if (DBJob.RetrieveJob(jobid) != null)
                {
                    Job j = DBJob.RetrieveJob(jobid);
                    lblJobTitle.Text = j.Title;
                    lblDescription.Text = j.Description;
                    lblType.Text = j.Type;
                    lblRequirements.Text = j.MinimumQualifications;
                    lblEmployer.Text = j.CompanyAccount.Company.CompanyName;
                    lblLocation.Text = j.Location;
                    lblTimestamp.Text = j.Timestamp.ToString();
                    lblIndustry.Text = j.Industry;
                    lblSalary.Text = j.EstimatedSalary;
                    lblWorkingHours.Text = j.WorkingHours;
                }
                else
                {
                    Response.Redirect("missingjob.aspx");
                }
            }
            else
            {
                Response.Redirect("missingjob.aspx");
            }
            
        }

        protected void linkBtnAddToWatchList_Click(object sender, EventArgs e)
        {
            string jobid = Request.QueryString["id"];
            DBJobseekerAccount.addJobsToWatchlist(ja, DBJob.RetrieveJob(jobid));
        }


        protected void btnBack_Click(object sender, EventArgs e)
        {
            object refUrl = ViewState["RefUrl"];
            if (refUrl != null)
                Response.Redirect((string)refUrl);
        }

        protected void linkBtnRecommendJobseeker_Click(object sender, EventArgs e)
        {
            /*
            JobseekerAccount js = DBJobseekerAccount.retrieveJobseekerAccount(User.Identity.Name, true);
            JobApplication ja = new JobApplication();
            Job j = DBJob.RetrieveJob((string)Session["jobid"]);
            ja.JAccountID = js.JAccountID;
            ja.JobID = (string)Session["jobid"];
            ja.Contents = txtContent.Text;
            ja.MarkedAsRead = "N";

            DBJobApplication.AddJobApplication(ja, j);
             */
        }


        protected void btnApply_Click(object sender, EventArgs e)
        {
            Session["company"] = lblEmployer.Text;
            Session["id"] = Request.QueryString["id"];
        }
    }
}
